package com.ytjj.qmyx.admin.controller;

import com.ytjj.common.api.CommonResult;
import com.ytjj.qmyx.admin.annotation.OperLog;
import com.ytjj.qmyx.admin.constants.SysLogConstants;
import com.ytjj.qmyx.admin.eunm.PromotePlatformEnums;
import com.ytjj.qmyx.admin.model.request.ChannelAddRequest;
import com.ytjj.qmyx.admin.model.request.ChannelRequest;
import com.ytjj.qmyx.admin.model.response.PromotePlatformResponse;
import com.ytjj.qmyx.admin.service.ChannelService;
import com.ytjj.qmyx.mall.model.Channel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 *
 *
 * @Version: 1.0
 *
 * 渠道管理
 */
@RestController
@RequestMapping("/channel")
public class ChannelController {

    @Autowired
    private ChannelService channelService;

    @PostMapping("/list")
    public CommonResult selectList() {
        return CommonResult.success(channelService.selectList());
    }

    @GetMapping("/getChannels")
    public CommonResult getChannels() {
        return CommonResult.success(channelService.getChannels());
    }

    @GetMapping("/getChannelInfoByChannel")
    public CommonResult getChannelInfoByChannel(@RequestParam("channel") String channel) {
        return CommonResult.success(channelService.getChannelInfoByChannel(channel));
    }

    @PostMapping("/getChannelList")
    @OperLog(operModule = "渠道管理", operDesc = "渠道列表", operType = SysLogConstants.OPERATION_SELECT)
    public CommonResult getChannelList(@RequestBody ChannelRequest channelRequest) {
        return CommonResult.success(channelService.getChannelList(channelRequest));
    }

    //好像暂时没在用得接口
    @PostMapping("/insertChannel")
    @OperLog(operModule = "渠道管理", operDesc = "新增渠道", operType = SysLogConstants.OPERATION_ADD)
    public CommonResult insertChannel(@RequestBody Channel channel) {
        int count = channelService.insertChannel(channel);
        if(count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @GetMapping("/updateChannelStatus")
    @OperLog(operModule = "渠道管理", operDesc = "渠道通过/不通过", operType = SysLogConstants.OPERATION_UPDATE)
    public CommonResult updateChannelStatus(@RequestParam("channelId") Integer channelId,
                                            @RequestParam("channelStatus") String channelStatus) {
        channelService.updateChannelStatus(channelId, channelStatus);
        return CommonResult.success();
    }

    @PostMapping("/addChannel")
    @OperLog(operModule = "渠道管理", operDesc = "新增渠道", operType = SysLogConstants.OPERATION_ADD)
    public CommonResult addChannel(@RequestBody @Validated ChannelAddRequest channelAddRequest) {
        if (channelAddRequest.getCreateTimes()>100){
            return CommonResult.failed("一次最多增加100条,请修改后重试");
        }
        for (int i = 0; i < channelAddRequest.getCreateTimes(); i++) {
            channelService.addChannel(channelAddRequest);
        }
        return CommonResult.success();
    }

    @PostMapping("/updateChannel")
    @OperLog(operModule = "渠道管理", operDesc = "编辑渠道", operType = SysLogConstants.OPERATION_UPDATE)
    public CommonResult updateChannel(@RequestBody ChannelRequest channelRequest) {
        int count = channelService.updateChannel(channelRequest);
        if(count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @GetMapping("/getChannelByPlat")
    public CommonResult getChannelList(@RequestParam String platform) {
        return CommonResult.success(channelService.getChannelByPlat(platform));
    }

    @GetMapping("/getChannelByPlatform")
    @OperLog(operModule = "渠道管理", operDesc = "根据渠道获取推广平台", operType = SysLogConstants.OPERATION_SELECT)
    public CommonResult getChannelByPlatform(@RequestParam String platform) {
        return CommonResult.success(channelService.getChannelByPlatform(platform));
    }

    @GetMapping("/removeData")
    @OperLog(operModule = "渠道管理", operDesc = "移除渠道", operType = SysLogConstants.OPERATION_DELETE)
    public CommonResult removeData(Integer id) {
        channelService.removeData(id);
        return CommonResult.success();
    }

    @PostMapping("/removeMoreData")
    @OperLog(operModule = "渠道管理", operDesc = "批量移除渠道", operType = SysLogConstants.OPERATION_DELETE)
    public CommonResult removeData(@RequestBody ChannelRequest channelRequest) {
        channelService.removeMoreData(channelRequest.getIds());
        return CommonResult.success();
    }

    @GetMapping("/getPromotePlatList")
    @OperLog(operModule = "渠道管理", operDesc = "获取所有推广平台", operType = SysLogConstants.OPERATION_SELECT)
    public CommonResult getPromotePlatList() {
        List<PromotePlatformResponse> list = PromotePlatformEnums.getplatList();
        return CommonResult.success(list);
    }

    @PostMapping("/insertPromotePlat")
    @OperLog(operModule = "渠道管理", operDesc = "新增渠道", operType = SysLogConstants.OPERATION_ADD)
    public CommonResult insertPromotePlat(@RequestBody ChannelRequest channelRequest) {
        channelService.insertPromotePlat(channelRequest);
        return CommonResult.success();
    }

    @GetMapping("/inChannelParam")
    @OperLog(operModule = "渠道管理", operDesc = "录入渠道参数", operType = SysLogConstants.OPERATION_UPDATE)
    public CommonResult inChannelParam(@RequestParam Integer id, @RequestParam String channelParam) {
        return CommonResult.success(channelService.inChannelParam(id, channelParam));
    }
}
